package com.ryder.petmatediarybackend.module.social.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.ryder.petmatediarybackend.common.response.Result;
import com.ryder.petmatediarybackend.module.social.dto.CreatePostDTO;
import com.ryder.petmatediarybackend.module.social.service.SocialPostService;
import com.ryder.petmatediarybackend.module.social.vo.PostVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 描述：社交动态管理 Controller
 *
 * @author Ryder
 * @version 1.0
 * @since 1.0
 * 更新日期: 2025-10-31 03:40:00
 */
@Slf4j
@Tag(name = "社交动态管理", description = "社交动态相关接口（需要登录）")
@RestController
@RequestMapping("/social")
@SecurityRequirement(name = "Authorization")
@RequiredArgsConstructor
public class SocialPostController {

    private final SocialPostService socialPostService;

    @Operation(summary = "创建动态", description = "用户发布新的社交动态")
    @PostMapping("/post/create")
    public Result<PostVO> createPost(@Valid @RequestBody CreatePostDTO dto) {
        Long userId = StpUtil.getLoginIdAsLong();
        log.info("[社交动态] 用户ID={} 执行创建动态, 方法=createPost, 入参={}", userId, dto);
        PostVO postVO = socialPostService.createPost(userId, dto);
        log.info("[社交动态] 用户ID={} 创建动态成功, 动态ID={}", userId, postVO.getId());
        return Result.ok(postVO);
    }

    @Operation(summary = "获取宠物圈子动态列表", description = "获取所有公开且已审核的宠物圈子动态（按时间倒序）")
    @GetMapping("/pet-circle/list")
    public Result<List<PostVO>> getPetCirclePosts() {
        Long userId = StpUtil.getLoginIdAsLong();
        log.info("[社交动态] 用户ID={} 执行获取宠物圈子动态列表, 方法=getPetCirclePosts", userId);
        List<PostVO> posts = socialPostService.getPetCirclePosts(userId);
        log.info("[社交动态] 用户ID={} 获取宠物圈子动态列表成功, 数量={}", userId, posts.size());
        return Result.ok(posts);
    }

    @Operation(summary = "获取动态详情", description = "根据动态ID获取详细信息（增加浏览量）")
    @GetMapping("/post/{postId}")
    public Result<PostVO> getPostDetail(@PathVariable Long postId) {
        Long userId = StpUtil.getLoginIdAsLong();
        log.info("[社交动态] 用户ID={} 执行获取动态详情, 方法=getPostDetail, 动态ID={}", userId, postId);
        PostVO postVO = socialPostService.getPostDetail(userId, postId);
        return Result.ok(postVO);
    }

    @Operation(summary = "删除动态", description = "逻辑删除自己的动态")
    @DeleteMapping("/post/delete/{postId}")
    public Result<Void> deletePost(@PathVariable Long postId) {
        Long userId = StpUtil.getLoginIdAsLong();
        log.info("[社交动态] 用户ID={} 执行删除动态, 方法=deletePost, 动态ID={}", userId, postId);
        socialPostService.deletePost(userId, postId);
        log.info("[社交动态] 用户ID={} 删除动态成功, 动态ID={}", userId, postId);
        return Result.ok();
    }

    @Operation(summary = "点赞动态", description = "为指定动态点赞（每个用户只能点赞一次）")
    @PostMapping("/post/like/{postId}")
    public Result<Void> likePost(@PathVariable Long postId) {
        Long userId = StpUtil.getLoginIdAsLong();
        log.info("[社交动态] 用户ID={} 执行点赞动态, 方法=likePost, 动态ID={}", userId, postId);
        socialPostService.likePost(userId, postId);
        log.info("[社交动态] 用户ID={} 点赞动态成功, 动态ID={}", userId, postId);
        return Result.ok();
    }

    @Operation(summary = "取消点赞", description = "取消对指定动态的点赞")
    @DeleteMapping("/post/unlike/{postId}")
    public Result<Void> unlikePost(@PathVariable Long postId) {
        Long userId = StpUtil.getLoginIdAsLong();
        log.info("[社交动态] 用户ID={} 执行取消点赞, 方法=unlikePost, 动态ID={}", userId, postId);
        socialPostService.unlikePost(userId, postId);
        log.info("[社交动态] 用户ID={} 取消点赞成功, 动态ID={}", userId, postId);
        return Result.ok();
    }
}

